package com.natasa.monitor.service.impl;

import java.io.IOException;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.springframework.http.HttpMethod;

import com.natasa.monitor.entity.monitor.HttpServiceMonitorRule;
import com.natasa.monitor.service.AbstractMonitorTask;
import com.natasa.monitor.util.HTTPUtils;
import com.natasa.monitor.util.JsonUtils;

public class HttpServiceMonitorTask extends AbstractMonitorTask {

	@Override
	protected String doMonitor() {
		HttpServiceMonitorRule httpServiceMonitorRule = (HttpServiceMonitorRule) moninorRule;
		String url = httpServiceMonitorRule.getUrl();
		String result = null;
		try {
			if(HttpMethod.POST == httpServiceMonitorRule.getMethod()){
				Map<String,String> parameter = JsonUtils.json2Map(httpServiceMonitorRule.getParameter());
				result = HTTPUtils.getResponseAsStringByPostMethod(url, parameter);
			}else{
				result = HTTPUtils.getResponseAsStringByGetMethod(url);
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

}
